package zombie.povray;
import java.io.*;
public class StreamGobbler extends Thread
{
    InputStream is;
    String type;
    boolean output;
    int printProgress = 0;
    
    public StreamGobbler(InputStream is, String type, boolean output)
    {
        this.is = is;
        this.type = type;
        this.output = output;
    }
    
    public void run()
    {
        try
        {
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String line=null;
            while ( (line = br.readLine()) != null)
            {    
                    if(output)System.out.println(line); 
                    
                    int lineIndex = line.indexOf("Rendering line");
                    if(lineIndex != -1)
                    {
                        int currentRender = new String("Rendering line").length() + lineIndex;
                        
                        char c[] = line.substring(currentRender).toCharArray();
                        int cLineNum = 0;
                        int tLineNum = 1;
                        boolean found = false;
                        int numStart = 0;
                        boolean done = false;
                        while (!done) {
                            if (c[numStart] == ' ')
                                numStart++;
                            else
                                done = true;
                        }
                        for (int i = numStart; i < c.length; i++) {
                          if (c[i] == ' ' && !found) {
                              found = true;
                              String currentLineNum = line.substring(currentRender + numStart, i + currentRender);
                              cLineNum = Integer.valueOf(currentLineNum);
                          } 
                        }
                        
                        int totalIndex = line.indexOf("of");
                        int totalRender = totalIndex + 2;
                        String totalLines = line.substring(totalRender);
                        char d[] = totalLines.toCharArray();
                        
                        done = false;
                        numStart = 0;
                        found = false;
                        
                        while (!done) {
                            if (d[numStart] == ' ')
                                numStart++;
                            else
                                done = true;
                        }
                        
                        for (int i = numStart; i < d.length; i++) {
                            if (d[i] == ' ' && !found) {
                                found = true;
                                String totalLineNum = line.substring(totalRender + numStart, i + totalRender);
                                tLineNum = Integer.valueOf(totalLineNum);
                            } 
                          }
                        if (printProgress == 0)
                            System.out.println("Frame render " + (int)(((float)cLineNum / (float)tLineNum) * 100.0f) + "% complete.");
                        printProgress = (printProgress < (tLineNum / 10))?printProgress + 1: 0;
                        
                    }
                   
                    
                }
            
            } catch (IOException ioe)
              {
                ioe.printStackTrace();  
              }
    }
}
